package com.demo.test.file.http;

import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.*;

/**
 * @author gy
 * @date 2023/2/20
 */
public class RemoteWebtrn {

    @Test
    public void m1() throws Exception {
        String url = "http://youxun.webtrn.cn/incoming/report/typxNew/71084ce32fb641d785e97e3b79354996/4028ab9d866770cc01866833e8600001/4028ab9d864db42e01864eed94330002/1676822118/9032b048e08f464eb2ff99ccdf304151.docx";
        this.downLoadPhoto(url);
    }


    public void  downLoadPhoto(String photoUrl)throws Exception{

        //获取照片返回二进制流
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        ResponseEntity<byte[]> entity = restTemplate.exchange(photoUrl, HttpMethod.GET,new HttpEntity<>(headers), byte[].class);
        byte[] body = entity.getBody();
        InputStream sbs = new ByteArrayInputStream(body);

        File dest = new File("D:\\test\\http\\aaa.docx");
        if(!dest.exists()) {
            dest.createNewFile();
        }

        InputStream in = null;
        OutputStream out = null;

        in = new ByteArrayInputStream(body);
        out = new FileOutputStream(dest);

        byte []bt = new byte[1024];
        int length=0;
        while(	(length = in.read(bt)) !=-1) {
            //一次性写入文件a中
            out.write(bt,0,length);
        }

        if(null!=out) {
            out.close();
        }



    }
}
